package com.block.datapush.form.item;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.common.util.BigDecimalUtil;
import com.block.common.util.RSAUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.form.item.util.ZhongXinUtil;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.FormInfoProfit;
import com.block.db.entity.MemberExtInfo;
import com.block.db.mapper.FormInfoProfitMapper;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 众鑫金服
 */
@Service
public class ZhongXinPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getCommLog();

    @Resource
    FormInfoProfitMapper formInfoProfitMapper;

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {
        try {

            String queryUrl = formInfo.getQueryUrl();

            Map<String, Object> bizParam = new HashMap<>();
            bizParam.put("mobile", memberExtInfo.getMobile());
            bizParam.put("apicode", formInfo.getApiKey());

            String sign = RSAUtil.rsaBASE64Encode(JSON.toJSONString(bizParam), formInfo.getPubKey());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("sign", sign);

            log.info("【众鑫金服】撞库开始【queryUrl】{}【bizParam】{}【paramMap】{}",
                    queryUrl, JSON.toJSONString(bizParam), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(queryUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【众鑫金服】撞库结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            String code = resultObject.getString("code");
            String msg = resultObject.getString("msg");

            if (StrUtil.equals(code, "1")){
                return true;
            }
            return false;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {
        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);
        try {

            String requestUrl = formInfo.getRequestUrl();

            Map<String, Object> bizParam = new HashMap<>();
            bizParam.put("apicode", formInfo.getApiKey());
            bizParam.put("mobile", memberExtInfo.getMobile());
            bizParam.put("name", memberExtInfo.getUsername());
//            bizParam.put("idcrad", memberExtInfo.getIdCard());
            bizParam.put("age", memberExtInfo.getAge()+"");
            bizParam.put("sex", memberExtInfo.getSex()==1?"1":"2");
            bizParam.put("zmf",Integer.valueOf(ZhongXinUtil.getZhima(memberExtInfo.getZhimaScore())));
            bizParam.put("location", ZhongXinUtil.getLocation(memberExtInfo));

            /**
             * 有社保,有公积金,有房产,有车产,有保险单,以上都没有,
             */
            List<String> _1028 = new ArrayList<>();
            if (StrUtil.equals(BaseTypeInfoEnum.SocialInsurance.si002.getValue(), memberExtInfo.getSocialInsurance())){
                _1028.add("有社保");
            }
            if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj002.getValue(), memberExtInfo.getGjj())){
                _1028.add("有公积金");
            }
            if (StrUtil.equals(BaseTypeInfoEnum.House.hu002.getValue(), memberExtInfo.getHouse())){
                _1028.add("有房产");
            }
            if (StrUtil.equals(BaseTypeInfoEnum.Car.car002.getValue(), memberExtInfo.getCar())){
                _1028.add("有车产");
            }
            if (StrUtil.equals(BaseTypeInfoEnum.Insurance.in002.getValue(), memberExtInfo.getInsurance())){
                _1028.add("有保险单");
            }

            if (CollUtil.isEmpty(_1028)){
                _1028.add("以上都没有");
            }
            bizParam.put("1028", _1028);

            String sign = RSAUtil.rsaBASE64Encode(JSON.toJSONString(bizParam), formInfo.getPubKey());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("sign", sign);

            log.info("【众鑫金服】导单开始【requestUrl】{}【bizParam】{}【paramMap】{}",
                    requestUrl, JSON.toJSONString(bizParam), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【众鑫金服】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);

            String code = resultObject.getString("code");
            String msg = resultObject.getString("msg");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(code));
            pushResultDTO.setMessage(msg);
            pushResultDTO.setParamContent(JSON.toJSONString(bizParam));
            if (StrUtil.equals(code, "1")){
                pushResultDTO.setSuccess(true);
            }else{
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return pushResultDTO;
    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {
    }
}
